package core.view.dirlist
{
	import core.event.CopyFileEvent;
	import core.view.CopyFileView;
	
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filesystem.File;
	import flash.text.TextField;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.listoy.ListoyItem;
	import org.aisy.skin.AisySkin;
	import org.aisy.ulist.UListEvent;

	internal class DirUListItem extends ListoyItem
	{
		protected var _textField:TextField;
		protected var _isLink:Boolean;
		protected var _isSelected:Boolean;
		protected var _w:Number;
		protected var _h:Number;
		
		public function DirUListItem(name:String, index:uint, data:*)
		{
			super(name, index, data);
			init();
		}
		
		protected function init():void
		{
			var h:String = "";
			for each (var i:String in itemInfo.keys) {
				switch (i) {
					case "targetList":
						h += i + ":\r" + getHtmlText(itemInfo[i], Op.DEFAULT);
						break;
					case "sourceList":
						h += i + ":\r" + getHtmlText(itemInfo[i], Op.COPY);
						break;
					default:
						h += getHtmlText(itemInfo[i], Op.DEFAULT);
						break;
				}
			}
			
			_textField = new TextField();
			_textField.defaultTextFormat = AisySkin.TEXTFORMAT();
			_textField.selectable = false;
			_textField.autoSize = "left";
			_textField.htmlText = h;
			if (_textField.width > Ais.IMain.stage.stageWidth - 18) {
				_textField.wordWrap = true;
				_textField.width = Ais.IMain.stage.stageWidth - 18;
			}
			addChild(_textField);
			
			_textField.addEventListener(TextEvent.LINK, __linkHandler);
			
			addEventListener(MouseEvent.ROLL_OVER, __mouseHandler);
			addEventListener(MouseEvent.ROLL_OUT, __mouseHandler);
			addEventListener(MouseEvent.CLICK, __mouseHandler);
			
			TEvent.newTrigger(NAME + "ITEM", __triggerHandler);
		}
		
		protected function getHtmlText(data:*, type:int, indent:String = ""):String
		{
			var str:String = "";
			if (data is Object) {
				for (var i:* in data) {
					str += indent + data[i];
					switch (type) {
						case Op.COPY:
							str += '    <a href="event:' + encodeURIComponent(JSON.stringify([type, i])) + '">Copy</a>';
							break;
					}
					str += '\r';
				}
			}
			else {
				str = data.toString();
			}
			return str;
		}
		
		protected function drawBg(color:uint, alpha:Number = 1):void
		{
			this.graphics.clear();
			this.graphics.beginFill(color, alpha);
			this.graphics.drawRoundRect(0, 0, _w, _h, 0);
			this.graphics.endFill();
		}
		
		protected function __mouseHandler(e:MouseEvent):void
		{
			switch (e.type) {
				case MouseEvent.ROLL_OVER:
					drawBg(0x009DFF);
					break;
				case MouseEvent.ROLL_OUT:
					_isSelected === true ? drawBg(0x4FA1DF) : drawBg(0x999999);
					break;
				case MouseEvent.CLICK:
					if (_isLink === false) {
						TEvent.trigger(NAME + "ITEM", UListEvent.RADIO_SELECT, index);
					}
					_isLink = false;
					break;
			}
		}
		
		protected function __linkHandler(e:TextEvent):void
		{
			_isLink = true;
			var arr:Array = JSON.parse(decodeURIComponent(e.text)) as Array;
			switch (arr[0]) {
				case Op.COPY:
					var arr2:Array;
					var f:File = new File(itemInfo.sourceList[arr[1]]);
					if (f.exists === true) {
						arr2 = f.isDirectory === true ? f.getDirectoryListing() : [f];
						f.cancel();
						TEvent.trigger(CopyFileEvent.COPY_FILE_EVENT, CopyFileEvent._CHANGE, itemInfo);
						CopyFileView.getInstance().copyFiles(arr2);
						TEvent.trigger(NAME + "ITEM", UListEvent.RADIO_SELECT, 0);
					}
					f = null;
					break;
			}
		}
		
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case UListEvent.RADIO_SELECT:
					if (index === data) {
						_isSelected = true;
						drawBg(0x4FA1DF);
						TEvent.trigger(NAME, UListEvent.RADIO_SELECT, itemInfo);
					}
					else {
						drawBg(0x999999);
						_isSelected = false;
					}
					break;
				case "GET_WIDTH_HEIGHT":
					data(width, height);
					break;
				case "SET_WIDTH_HEIGHT":
					_w = data[0];
					_h = this.height;
					break;
			}
			type = null;
			data = null;
		}
		
		override public function clear():void
		{
			_textField.removeEventListener(TextEvent.LINK, __linkHandler);
			_textField = null;
			super.clear();
		}
		
	}
}

internal class Op
{
	static public const DEFAULT:int = 0;
	static public const COPY:int = 1;
}